<?php
/*
 * @Date: 2021-05-06 22:02:36
 * @Author: yukun
 * @LastEditors: yukun
 * @LastEditTime: 2021-05-06 22:17:54
 * @FilePath: /design-patterns-study/单例模式/single.php
 * @Description: 本文件主要功能为：
 */
class Single
{
    protected static $ins = null;
    
    // 定义protected类型是为了防止外部类自己去 new 对象 定义为 final 则方法不能被覆盖
    final protected function __construct()
    {
        
    }

    // 防止被复写，类被克隆
    final protected function __clone()
    {
        # code...
    }

    public static function getIns() {
        if (self::$ins == null) {
            self::$ins = new self();
        }

        return self::$ins;
    }
}


$single1 = Single::getIns();
$single2 = Single::getIns();

if ($single1 == $single2) {
    echo "同一个对象" . PHP_EOL;
} else {
    echo "不是同一个对象" . PHP_EOL;
}